/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package data;

import java.util.Hashtable;
import model.MailQueue;
import smtp.MIMEutility;

/**
 * @author lailiaidi
 * This class is to record all the mail that has been sent through the application
 */
public class DataQueue {

    private Hashtable obQueueHashMail;
    public final int STATUS_SENT = 0;
    public final int STATUS_DELIVERED = 1;
    public final int STATUS_FAILED = 2;

    public DataQueue() {
        obQueueHashMail = new Hashtable();
    }

    public void addMailQueueData(String obStrSender, String obStrReceiver, String obStrSubject, String obStrSendingTime, String obStrDeliveryTime, int obIntDeliveryStatus, String setDeliveryDetail) {
        this.addMailQueueData(obStrSender, obStrReceiver, obStrSubject, null, obStrSendingTime, obStrDeliveryTime, obIntDeliveryStatus, setDeliveryDetail);
    }

    public void addMailQueueData(String obStrSender, String obStrReceiver, String obStrSubject, String obStrSubmitTime, String obStrSendingTime, String obStrDeliveryTime, int obIntDeliveryStatus, String setDeliveryDetail) {
        MailQueue obMailQueue = new MailQueue();

        if (obStrSender.contains("<")) {
            obStrSender = obStrSender.replaceAll("<", "");
        }
        if (obStrSender.contains(">")) {
            obStrSender = obStrSender.replaceAll(">", "");
        }
        obMailQueue.setSender(obStrSender);
        if (obStrReceiver.contains("<")) {
            obStrReceiver = obStrReceiver.replaceAll("<", "");
        }
        if (obStrReceiver.contains(">")) {
            obStrReceiver = obStrReceiver.replaceAll(">", "");
        }
        obMailQueue.setReceiver(obStrReceiver);
        MIMEutility obMIMEutility = new MIMEutility();
        obMailQueue.setSubject(obMIMEutility.MIMEDecodingSubject(obStrSubject));
        if (obStrDeliveryTime == null) {
            obMailQueue.setDeliveryTime("unknown");
        } else {
            obMailQueue.setDeliveryTime(obStrDeliveryTime);
        }
        obMailQueue.setSubmitDate(obStrSubmitTime);
        obMailQueue.setSendingTime(obStrSendingTime);
        obMailQueue.setDeliveryStatus(obIntDeliveryStatus);
        obMailQueue.setDeliveryDetail(setDeliveryDetail);

        obQueueHashMail.put(obStrSendingTime, obMailQueue);
    }

    public void updateMailQueueData(String obStrSendingTime, String obStrDeliveryTime, int obIntDeliveryStatus, String obDeliveryDetail) {
        MailQueue obMailQueue = (MailQueue) obQueueHashMail.get(obStrSendingTime);
        obMailQueue.setDeliveryDetail(obDeliveryDetail);
        obMailQueue.setDeliveryStatus(obIntDeliveryStatus);
        obMailQueue.setDeliveryTime(obStrDeliveryTime);
        //REMOVE THE OLD DATA
        obQueueHashMail.remove(obStrSendingTime);
        //PUT THE NEW DATA
        obQueueHashMail.put(obStrSendingTime, obMailQueue);
    }

    public Hashtable getMailQueue() {
        return obQueueHashMail;
    }

    public int numberOfMessages() {
        return obQueueHashMail.size();
    }
}
